/*
 * BlockDesc.h
 *
 *  Created on: Mar 23, 2010
 *      Author: Lukasz Spintzyk
 */

#ifndef BLOCKGROUP_H_
#define BLOCKGROUP_H_

#include <boost/dynamic_bitset.hpp>

#ifndef SUPERBLOCK_H_
#include "SuperBlock.h"
#endif

class IOStream;
class Inode;
class BlockManager;

class BlockDesc
{
public:
	friend class BlockManager;

    //BlockDesc();
    BlockDesc(uint32_t nb);
    virtual ~BlockDesc();

    //bool getInode(uint32_t nb, Inode&);
    //bool setInode(uint32_t nb, const Inode&);
    void setInode(uint32_t nr, bool bit);
    bool hasBlock(uint32_t nr);
    bool hasInode(uint32_t nr);
    void getBlock(uint32_t nr, void* buffer);

    uint32_t getFreeInode();

    void readDesc(byte* buffer);
    bool writeDesc(byte* buffer);

    void setSuperBlock(const SuperBlock& sb);
    SuperBlock& getSuperBock();

    //void setBuffer(boost::shared_ptr<byte> buffer);

    void setGroupNumber(uint32_t number);
    uint32_t getGroupNumber();

    uint32_t sizeInBlocks();
    uint32_t inodeTableSize();
    uint32_t reservedBlocks();
    uint32_t freeBlocks();

    bool tryReserveBlocks(const uint32_t num, uint32_t& retBlock,const uint32_t after_bock = 0);

protected:
	void updateStatValues();

private:
    void setBlock(uint32_t nr, const void* buffer);
	BlockDesc(const BlockDesc&);

    SuperBlock d_sb;
    boost::dynamic_bitset<byte> d_blocksBitMap;
    boost::dynamic_bitset<byte> d_inodeBitmap;

    uint32_t d_groupNumber;
    uint32_t d_blockBitmapNb;
    uint32_t d_inodeBitmapNb;
    uint32_t d_inodeTableBlockNb;

    uint32_t d_freeBlocks;
    uint32_t d_freeInodes;
    ///! Value indicating number of inodes allocated to directories for the represented group
    uint32_t d_usedDrectoryCount;
	

    uint32_t d_gOffset;

    bool d_superBlockModified;
};

#endif /* BLOCKGROUP_H_ */
